﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using LDCRNewEdit.Model;
using LDCRNewEdit.Services;

namespace LDCRNewEdit.Utils
{
    public class Resources
    {
        /// <summary>
        /// 游戏目录
        /// </summary>
        public static string GamePath;

        private static Dictionary<string, Dictionary<string, string>> _FilePathMap = new Dictionary<string, Dictionary<string, string>>();

        public static string GetPath(string key, bool isfull)
        {
            if (isfull)
            {
                return (GamePath + GetPath(key, key)).Replace("/", "\\");
            }
            else
            {
                return GetPath(key, key);
            }
        }

        public static string GetPath(string key)
        {
            return GetPath(key, true);
        }
        public static string GetPath(string key, string okey)
        {
            return _FilePathMap[key][okey];
        }
        public static Dictionary<string, string> GetPaths(string key)
        {
            return _FilePathMap[key];
        }


        public static void LoadXML(string xmlFile)
        {
            XmlDocument dom = new XmlDocument();
            dom.Load(xmlFile);//装载XML文档 

            XmlNode root = dom.ChildNodes[1];

            foreach (XmlNode xe in root.ChildNodes)
            {
                if (xe.GetType() == typeof(XmlComment))
                    continue;
                Dictionary<string, string> path = new Dictionary<string, string>();
                if (xe.HasChildNodes && xe.ChildNodes[0].GetType() != typeof(XmlText))
                {
                    foreach (XmlNode child in xe.ChildNodes)
                    {
                        if (xe.Name == "BaseDataPath")
                        {
                            BaseDataPath basepath = new BaseDataPath();
                            basepath.Name = child.Name;
                            basepath.ID = child.Attributes["id"].Value;
                            basepath.DatFilename = GamePath + child.Attributes["d"].Value;
                            basepath.XmlFilename = GamePath + child.Attributes["x"].Value;

                            BaseDataService.BaseDataPathList.Add(basepath.ID, basepath);
                        }

                        path.Add(child.Attributes["id"].Value, child.InnerText);
                    }

                }
                else
                {
                    path.Add(xe.Name, xe.InnerText);
                }

                _FilePathMap.Add(xe.Name, path);

            }
        }
    }
}
